package com.proj.controller;

import com.proj.bean.*;
import com.proj.service.IGoodsService;
import com.proj.util.LayData;
import com.proj.util.Page;
import com.proj.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    IGoodsService goodsService;


    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text){
        List<Goods> list = goodsService.getList(page, text);
        int count=goodsService.getCount(text);
        return new LayData(count, list);
    }


    @RequestMapping("getImgByGoodsid")
    @ResponseBody
    public List<Goodsimg> getImgByGoodsid(int goodsid) {
        return goodsService.getImgByGoodsid(goodsid);
    }

    @RequestMapping("insertImg")
    @ResponseBody
    public int insertImg(int goodsid, String imgurl) {
        return goodsService.insertImg(goodsid, imgurl);
    }

    @RequestMapping("delImgById")
    @ResponseBody
    public int delImgById(int id) {
        return goodsService.delImgById(id);
    }



    @RequestMapping("goodsList")
    @ResponseBody
    public List<Goods> goodsList(Page page, String text){
        return goodsService.getList(page, text);
    }
    @RequestMapping("goodsLists")
    @ResponseBody
    public List<Goods> goodsLists(@RequestParam(value="categoryid",required=false) Integer categoryid,@RequestParam(value ="keywords",required=false) String keywords){
        return goodsService.goodsList(categoryid, keywords);
    }
    @RequestMapping("getById")
    @ResponseBody
    public Goods getById(int id){
        return goodsService.getById(id);
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(Goods goods){
        return goodsService.add(goods);
    }

    @RequestMapping("del")
    @ResponseBody
    public int del(int id){
        return goodsService.del(id);
    }


    @RequestMapping("edit")
    @ResponseBody
    public int edit(Goods goods){
        return goodsService.edit(goods);
    }


    @RequestMapping("getparent")
    @ResponseBody
    public List<Category> getParent(int category_id){
        return goodsService.getParent(category_id);
    }

    @RequestMapping("reviewlist")
    @ResponseBody
    public List<Review> reviewlist(int goodsid){
        return goodsService.reviewlist(goodsid);
    }

    @RequestMapping("getimg")
    @ResponseBody
    public List<Goodsimg> getimg(int goodsid){
        return goodsService.getimg(goodsid);
    }

    @ResponseBody
    @RequestMapping("addsingletocart")
    public int addsingletocart(int goodsid,int number,HttpSession session){
        return goodsService.addsingletocart(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId(),goodsid,number);
    }

    @ResponseBody
    @RequestMapping("ifbought")
    public int ifbought(int goodsid,HttpSession session){
        int userid=((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId();
        return goodsService.ifbought(userid,goodsid);
    }

    @ResponseBody
    @RequestMapping("newreview")
    public int newreview(Review review,HttpSession session){
        review.setUserid(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId());
        return goodsService.newreview(review);
    }

    @ResponseBody
    @RequestMapping("getReviewByUserid")
    public List<Review> getReviewByUserid(HttpSession session) {
        return goodsService.getReviewByUserid(((User) session.getAttribute(SYS.SHOPPING_LOGIN_USER)).getId());
    }

    @ResponseBody
    @RequestMapping("delReviewById")
    public int delReviewById(int id) {
        return goodsService.delReviewById(id);
    }

}
